package cn.tedu.fresheveryday.mapper;

import cn.tedu.fresheveryday.pojo.entity.Comment;
import cn.tedu.fresheveryday.pojo.vo.CommentVO;
import cn.tedu.fresheveryday.pojo.vo.UserSimpleVO;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface CommentMapper {
    /**
     * 添加评论
     *
     * @param comment
     */
    @Insert("insert into comment (id, content, type, user_id, comment_id, created)" +
            "values(#{id}, #{content}, #{type}, #{userId}, #{commentId}, #{created})")
    void insert(Comment comment);

    /**
     * 通过商品id查询评论
     *
     * @param productId
     * @return
     */
    @Select("select id, content, type, user_id, comment_id, created from comment " +
            "where product_id = #{productId}")
    @Result(column = "id", property = "id")
    @Result(column = "comment_id", property = "commentId")
    @Result(
            column = "id", property = "images",
            many = @Many(select = "cn.tedu.fresheveryday.mapper.CommentImageMapper.selectByCommentId")
    )
    @Result(
            column = "user_id", property = "user", javaType = UserSimpleVO.class,
            one = @One(select = "cn.tedu.fresheveryday.mapper.UserMapper.selectSimpleUserById")
    )
    List<CommentVO> selectByProductId(int productId);
}